/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package proyecto;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;


//creamos las variables que necesitamos para la clase
public class Cambiocontra extends javax.swing.JDialog {
String inicial;
       Connection cn;
   Statement st;
    ResultSet rs;
    PreparedStatement pst;
    
    /*utilizamos un constructor con el que iniciaremos nuestro jdialog 
    que nos recibira estos parametros principalmente el de inicial para 
    poder reconocer al usuario que ingreso 
    */
    public Cambiocontra(java.awt.Frame parent, boolean modal,String inicial) {
        super(parent, modal);
        initComponents();
        this.inicial=inicial;
        txtinicial.setText(inicial);
        this.setTitle("Cambio contraseña");
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
        cn = DriverManager.getConnection("jdbc:odbc:noko","Nokomotion","sasa");
   
    }catch(Exception e){}
 
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtrecontra = new javax.swing.JPasswordField();
        txtcontra = new javax.swing.JPasswordField();
        btnaceptar = new javax.swing.JButton();
        txtinicial = new javax.swing.JTextField();
        btncancelar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Contraseña nueva");

        jLabel2.setText("Repite contraseña");

        txtrecontra.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtrecontraKeyTyped(evt);
            }
        });

        txtcontra.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtcontraKeyTyped(evt);
            }
        });

        btnaceptar.setText("Aceptar");
        btnaceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaceptarActionPerformed(evt);
            }
        });

        txtinicial.setEditable(false);

        btncancelar.setText("Cancelar");
        btncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelarActionPerformed(evt);
            }
        });

        jLabel3.setText("La contraseña hasta 8 caracteres");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(79, 79, 79)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txtcontra, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(txtrecontra, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(txtinicial, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel3)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(btncancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addComponent(btnaceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(txtinicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtcontra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtrecontra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnaceptar)
                    .addComponent(btncancelar))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
//creamos un update para el cambio de contraseña
    private void btnaceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaceptarActionPerformed
      String contra=txtcontra.getText();
        if(contra.equals( txtrecontra.getText())){
            try{
                
                 pst=cn.prepareStatement("Update Usuarios Set Password='"+contra +"'Where Inicial='"+inicial+"'");
                 pst.executeUpdate();
                 Borrar();
                 
        }catch (Exception e)
        {
            JOptionPane.showMessageDialog(null,"Conexion no exitosa"+e);
        }
        }else
        {
            JOptionPane.showMessageDialog(null,"La contraseña no es igual");
        }
        
        
        
    }//GEN-LAST:event_btnaceptarActionPerformed
public void Borrar(){
    txtcontra.setText("");
    txtrecontra.setText("");
}
    
    private void btncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelarActionPerformed
     
       this.dispose();
    }//GEN-LAST:event_btncancelarActionPerformed
//Restringe hasta 8 caracteres
    private void txtrecontraKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtrecontraKeyTyped
if (txtrecontra.getText().length() ==8)
          {
            evt.consume();
          }
    }//GEN-LAST:event_txtrecontraKeyTyped
//Restringe hasta 8 caracteres
    private void txtcontraKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcontraKeyTyped
       if (txtcontra.getText().length() ==8)
          {
            evt.consume();
          }
    }//GEN-LAST:event_txtcontraKeyTyped

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnaceptar;
    private javax.swing.JButton btncancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField txtcontra;
    private javax.swing.JTextField txtinicial;
    private javax.swing.JPasswordField txtrecontra;
    // End of variables declaration//GEN-END:variables
}
